package com.example.statemachine.controller;

import com.example.statemachine.entity.Credential;
import com.example.statemachine.service.CredentialService;
import com.example.statemachine.util.MessageBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * @author jianwei.wang
 * @version v1.0
 * @date 2019/5/6 16:16
 **/
@Slf4j
@RestController
public class CredentialController {


    @Autowired
    private CredentialService credentialService;


    @RequestMapping("/all")
    public MessageBody all() {
        return MessageBody.success(credentialService.all());
    }

    @RequestMapping("/add")
    public MessageBody add() {
        Credential credential = new Credential();
        credential.setId(UUID.randomUUID().toString());
        credential.setGoodsId(UUID.randomUUID().toString().replaceAll("-", ""));
        credential.setGoodsName("wjwei_" + Math.abs(new Random().nextInt()));
        credential.setValidDate(new Date());
        credentialService.tempStorage(credential);
        return MessageBody.success();
    }

    @RequestMapping("/submit")
    public MessageBody submit(@RequestBody Credential credential) {
        credentialService.submit(credential);
        return MessageBody.success();
    }

    @RequestMapping("/withDraw")
    public MessageBody withDraw(@RequestBody Credential credential) {
        credentialService.withDraw(credential);
        return MessageBody.success();
    }

    @RequestMapping("/review")
    public MessageBody review(@RequestBody Credential credential) {
        credentialService.review(credential);
        return MessageBody.success();
    }

    @RequestMapping("/antiReview")
    public MessageBody antiReview(@RequestBody Credential credential) {
        credentialService.antiReview(credential);
        return MessageBody.success();
    }
}
